package com.htjx.mall.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;

public class Dates {
	private static final String FORMAT_TIMESTAMP = "yyyy-MM-dd HH:mm:ss.SSS";
	private static final String FORMAT_TIME = "yyyy-MM-dd HH:mm:ss";
	private static final String FORMAT_TIME_STRING = "yyyyMMddHHmmssSSS";

	private static String[] parsePatterns;
	static {
		parsePatterns = new String[] { "yyyyMMdd", "yyyy/MM/dd", "yyyy-MM-dd", "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:ss", "YYYY-MM-DD hh:mm:ss", "yyyy/MM/dd HH:mm" };
	}

	public static Date parse(String string) {
		try {
			return DateUtils.parseDate(string, parsePatterns);
		} catch (ParseException e) {
			throw new IllegalArgumentException(e);
		}
	}

	public static String toTime(Date date) {
		return new SimpleDateFormat(FORMAT_TIME).format(date);
	}

	public static String toTimestamp(Date date) {
		return new SimpleDateFormat(FORMAT_TIMESTAMP).format(date);
	}
	
	public static String toTimeString(Date date) {
		return new SimpleDateFormat(FORMAT_TIME_STRING).format(date);
	}

	public static String toDate(Date date) {
		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}

	public static String toYYMMDD(Date date) {
		return new SimpleDateFormat("yyMMdd").format(date);
	}

	public static String toYYYYMMDD(Date date) {
		return new SimpleDateFormat("yyyyMMdd").format(date);
	}

	public static String toYYYY_MM_DD(Date date) {
		return new SimpleDateFormat("yyyy-MM-dd").format(date.getTime());
	}

	public static Calendar toCalnedar(Date date) {
		Calendar result = Calendar.getInstance();
		result.setTime(date);
		return result;
	}

	/**
	 * 获取日期的前后多少天
	 * 
	 * @param date
	 * @param add
	 * @return
	 */
	public static Calendar get(Calendar date, int add) {
		Calendar result = Calendar.getInstance();
		result.setTime(date.getTime());
		result.add(Calendar.DAY_OF_MONTH, add);
		return result;
	}
	
	/**
	 * 返回此日期的零点整，如2007-3-14 19:00:35 返回 2007-3-14 00:00:00
	 * 
	 * @param date
	 */
	public static Date getTheDayZero(Date date) {
		Date result = DateUtils.setHours(date, 0);
		result = DateUtils.setMinutes(result, 0);
		result = DateUtils.setSeconds(result, 0);
		result = DateUtils.setMilliseconds(result, 0);
		return result;
	}

	/**
	 * 返回此日期的午夜，如2007-3-14 19:00:35 返回 2007-3-14 23:59:59
	 * 
	 * @param date
	 */
	public static Date getTheDayMidnight(Date date) {
		Date result = DateUtils.setHours(date, 23);
		result = DateUtils.setMinutes(result, 59);
		result = DateUtils.setSeconds(result, 59);
		result = DateUtils.setMilliseconds(result, 999);
		return result;
	}

	/** 判断两日期之间的天数差 **/
	public static int diff(Date one, Date two) {
		int days = 0;
		Date small, big;
		if (one.before(two)) {
			small = (Date) one.clone();
			big = (Date) two.clone();
		} else {
			big = (Date) one.clone();
			small = (Date) two.clone();
		}
		while (!DateUtils.isSameDay(small, big)) {
			days++;
			small = DateUtils.addDays(small, 1);
		}
		return days;
	}

	public static int getYear(Date date) {
		return toCalnedar(date).get(Calendar.YEAR);
	}

	public static int getMonth(Date date) {
		return toCalnedar(date).get(Calendar.MONTH);
	}

	public static int getDay(Date date) {
		return toCalnedar(date).get(Calendar.DAY_OF_MONTH);
	}

	public static int getWeek(Date date) {
		return toCalnedar(date).get(Calendar.DAY_OF_WEEK);
	}

	public static int getHour(Date date) {
		return toCalnedar(date).get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(Date date) {
		return toCalnedar(date).get(Calendar.MINUTE);
	}

	public static int getSecond(Date date) {
		return toCalnedar(date).get(Calendar.SECOND);
	}
	
	public static int getMilliSecond(Date date) {
		return toCalnedar(date).get(Calendar.MILLISECOND);
	}
	
	/**
	 * 指定日期加1年
	 * 
	 * @param date
	 * @param add
	 * @return
	 */
	public static Date nextYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, 1);
		return cal.getTime();
	}
}
